const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

// 基础权限列表
const permissions = [
  // 用户权限
  { code: 'view:users', name: '查看用户', group: 'users', description: '查看用户列表和详情' },
  { code: 'create:users', name: '创建用户', group: 'users', description: '创建新用户' },
  { code: 'update:users', name: '更新用户', group: 'users', description: '编辑用户信息' },
  { code: 'delete:users', name: '删除用户', group: 'users', description: '删除现有用户' },
  
  // 角色权限
  { code: 'view:roles', name: '查看角色', group: 'roles', description: '查看角色列表和详情' },
  { code: 'create:roles', name: '创建角色', group: 'roles', description: '创建新角色' },
  { code: 'update:roles', name: '更新角色', group: 'roles', description: '编辑角色信息' },
  { code: 'delete:roles', name: '删除角色', group: 'roles', description: '删除现有角色' },
  
  // 部门权限
  { code: 'view:departments', name: '查看部门', group: 'departments', description: '查看部门列表和详情' },
  { code: 'create:departments', name: '创建部门', group: 'departments', description: '创建新部门' },
  { code: 'update:departments', name: '更新部门', group: 'departments', description: '编辑部门信息' },
  { code: 'delete:departments', name: '删除部门', group: 'departments', description: '删除现有部门' },
  
  // 商品权限
  { code: 'view:products', name: '查看商品', group: 'products', description: '查看商品列表和详情' },
  { code: 'create:products', name: '创建商品', group: 'products', description: '创建新商品' },
  { code: 'update:products', name: '更新商品', group: 'products', description: '编辑商品信息' },
  { code: 'delete:products', name: '删除商品', group: 'products', description: '删除现有商品' },
  
  // 分类权限
  { code: 'view:categories', name: '查看分类', group: 'categories', description: '查看分类列表和详情' },
  { code: 'create:categories', name: '创建分类', group: 'categories', description: '创建新分类' },
  { code: 'update:categories', name: '更新分类', group: 'categories', description: '编辑分类信息' },
  { code: 'delete:categories', name: '删除分类', group: 'categories', description: '删除现有分类' },
  
  // 订单权限
  { code: 'view:orders', name: '查看订单', group: 'orders', description: '查看订单列表和详情' },
  { code: 'create:orders', name: '创建订单', group: 'orders', description: '创建新订单' },
  { code: 'update:orders', name: '更新订单', group: 'orders', description: '编辑订单信息' },
  { code: 'delete:orders', name: '删除订单', group: 'orders', description: '删除现有订单' },
  
  // 库存管理权限
  { code: 'view:inventory', name: '查看库存', group: 'inventory', description: '查看库存信息' },
  { code: 'inventory:update', name: '更新库存', group: 'inventory', description: '更新商品库存信息' },
  { code: 'inventory:adjust', name: '调整库存', group: 'inventory', description: '手动调整库存数量' },
  
  // 仓库管理权限
  { code: 'view:warehouses', name: '查看仓库', group: 'inventory', description: '查看仓库列表和详情' },
  { code: 'warehouse:create', name: '创建仓库', group: 'inventory', description: '创建新仓库' },
  { code: 'warehouse:update', name: '更新仓库', group: 'inventory', description: '编辑仓库信息' },
  { code: 'warehouse:delete', name: '删除仓库', group: 'inventory', description: '删除仓库' },
  
  // 供应商管理权限
  { code: 'view:suppliers', name: '查看供应商', group: 'inventory', description: '查看供应商列表和详情' },
  { code: 'supplier:create', name: '创建供应商', group: 'inventory', description: '创建新供应商' },
  { code: 'supplier:update', name: '更新供应商', group: 'inventory', description: '编辑供应商信息' },
  { code: 'supplier:delete', name: '删除供应商', group: 'inventory', description: '删除供应商' },
  
  // 入库单管理
  { code: 'view:stock-in', name: '查看入库单', group: 'inventory', description: '查看入库单列表和详情' },
  { code: 'stock-in:create', name: '创建入库单', group: 'inventory', description: '创建新入库单' },
  { code: 'stock-in:update', name: '更新入库单', group: 'inventory', description: '编辑入库单' },
  { code: 'stock-in:delete', name: '删除入库单', group: 'inventory', description: '删除入库单' },
  
  // 出库单管理
  { code: 'view:stock-out', name: '查看出库单', group: 'inventory', description: '查看出库单列表和详情' },
  { code: 'stock-out:create', name: '创建出库单', group: 'inventory', description: '创建新出库单' },
  { code: 'stock-out:update', name: '更新出库单', group: 'inventory', description: '编辑出库单' },
  { code: 'stock-out:delete', name: '删除出库单', group: 'inventory', description: '删除出库单' },
  
  // 库存盘点
  { code: 'view:inventory-check', name: '查看盘点', group: 'inventory', description: '查看库存盘点记录' },
  { code: 'inventory-check:create', name: '创建盘点', group: 'inventory', description: '创建新的库存盘点' },
  { code: 'inventory-check:update', name: '更新盘点', group: 'inventory', description: '编辑库存盘点' },
  
  // 系统权限
  { code: 'view:settings', name: '查看设置', group: 'settings', description: '查看系统设置' },
  { code: 'update:settings', name: '更新设置', group: 'settings', description: '更新系统设置' },
  { code: 'view:reports', name: '查看报表', group: 'reports', description: '查看系统报表' },
  
  // 所有权限
  { code: 'all', name: '所有权限', group: 'system', description: '系统所有权限' },
  
  // 库存管理权限
  { code: 'inventory:view', name: '查看库存', group: 'inventory', description: '查看库存' },
  { code: 'inventory:edit', name: '编辑库存信息', group: 'inventory', description: '编辑库存信息' },
  { code: 'inventory:adjust', name: '调整库存', group: 'inventory', description: '调整库存' },
  { code: 'inventory:warehouse:view', name: '查看仓库', group: 'inventory', description: '查看仓库' },
  { code: 'inventory:warehouse:create', name: '创建仓库', group: 'inventory', description: '创建仓库' },
  { code: 'inventory:warehouse:edit', name: '编辑仓库', group: 'inventory', description: '编辑仓库' },
  { code: 'inventory:warehouse:delete', name: '删除仓库', group: 'inventory', description: '删除仓库' },
  { code: 'inventory:supplier:view', name: '查看供应商', group: 'inventory', description: '查看供应商' },
  { code: 'inventory:supplier:create', name: '创建供应商', group: 'inventory', description: '创建供应商' },
  { code: 'inventory:supplier:edit', name: '编辑供应商', group: 'inventory', description: '编辑供应商' },
  { code: 'inventory:supplier:delete', name: '删除供应商', group: 'inventory', description: '删除供应商' },
  { code: 'inventory:stockIn:view', name: '查看入库单', group: 'inventory', description: '查看入库单' },
  { code: 'inventory:stockIn:create', name: '创建入库单', group: 'inventory', description: '创建入库单' },
  { code: 'inventory:stockIn:edit', name: '编辑入库单', group: 'inventory', description: '编辑入库单' },
  { code: 'inventory:stockIn:delete', name: '删除入库单', group: 'inventory', description: '删除入库单' },
  { code: 'inventory:stockIn:confirm', name: '确认入库单', group: 'inventory', description: '确认入库单' },
  { code: 'inventory:stockIn:cancel', name: '取消入库单', group: 'inventory', description: '取消入库单' },
  { code: 'inventory:stockOut:view', name: '查看出库单', group: 'inventory', description: '查看出库单' },
  { code: 'inventory:stockOut:create', name: '创建出库单', group: 'inventory', description: '创建出库单' },
  { code: 'inventory:stockOut:edit', name: '编辑出库单', group: 'inventory', description: '编辑出库单' },
  { code: 'inventory:stockOut:delete', name: '删除出库单', group: 'inventory', description: '删除出库单' },
  { code: 'inventory:stockOut:confirm', name: '确认出库单', group: 'inventory', description: '确认出库单' },
  { code: 'inventory:stockOut:cancel', name: '取消出库单', group: 'inventory', description: '取消出库单' }
];

// 基础角色
const roles = [
  { 
    name: 'admin', 
    description: '系统管理员，拥有所有权限', 
    isSystem: true,
    permissions: ['all'] 
  },
  { 
    name: 'manager', 
    description: '经理，拥有大部分权限，但不能管理系统设置', 
    isSystem: true,
    permissions: [
      'view:users', 'create:users', 'update:users',
      'view:roles',
      'view:departments', 'create:departments', 'update:departments',
      'view:products', 'create:products', 'update:products', 'delete:products',
      'view:categories', 'create:categories', 'update:categories', 'delete:categories',
      'view:orders', 'create:orders', 'update:orders', 'delete:orders',
      'view:reports',
      // 库存管理权限
      'view:inventory', 'inventory:update', 'inventory:adjust',
      'view:warehouses', 'warehouse:create', 'warehouse:update', 'warehouse:delete',
      'view:suppliers', 'supplier:create', 'supplier:update', 'supplier:delete',
      'view:stock-in', 'stock-in:create', 'stock-in:update', 'stock-in:delete',
      'view:stock-out', 'stock-out:create', 'stock-out:update', 'stock-out:delete',
      'view:inventory-check', 'inventory-check:create', 'inventory-check:update',
      'inventory:view', 'inventory:edit',
      'inventory:warehouse:view', 'inventory:warehouse:create', 'inventory:warehouse:edit', 'inventory:warehouse:delete',
      'inventory:supplier:view', 'inventory:supplier:create', 'inventory:supplier:edit', 'inventory:supplier:delete',
      'inventory:stockIn:view', 'inventory:stockIn:create', 'inventory:stockIn:edit', 'inventory:stockIn:delete', 'inventory:stockIn:confirm', 'inventory:stockIn:cancel',
      'inventory:stockOut:view', 'inventory:stockOut:create', 'inventory:stockOut:edit', 'inventory:stockOut:delete', 'inventory:stockOut:confirm', 'inventory:stockOut:cancel'
    ] 
  },
  { 
    name: 'staff', 
    description: '收银员，只能进行销售相关操作', 
    isSystem: true,
    permissions: [
      'view:products', 
      'view:categories',
      'view:orders', 'create:orders', 'update:orders',
      'view:inventory' // 只允许查看库存
    ] 
  },
  {
    name: 'inventory_manager',
    description: '库存管理员，负责库存管理相关操作',
    isSystem: true,
    permissions: [
      'view:products',
      'view:categories',
      'view:inventory', 'inventory:update', 'inventory:adjust',
      'view:warehouses', 'warehouse:create', 'warehouse:update',
      'view:suppliers', 'supplier:create', 'supplier:update',
      'view:stock-in', 'stock-in:create', 'stock-in:update',
      'view:stock-out', 'stock-out:create', 'stock-out:update',
      'view:inventory-check', 'inventory-check:create', 'inventory-check:update',
      'inventory:view', 'inventory:edit',
      'inventory:warehouse:view', 'inventory:warehouse:create', 'inventory:warehouse:edit', 'inventory:warehouse:delete',
      'inventory:supplier:view', 'inventory:supplier:create', 'inventory:supplier:edit', 'inventory:supplier:delete',
      'inventory:stockIn:view', 'inventory:stockIn:create', 'inventory:stockIn:edit', 'inventory:stockIn:delete', 'inventory:stockIn:confirm', 'inventory:stockIn:cancel',
      'inventory:stockOut:view', 'inventory:stockOut:create', 'inventory:stockOut:edit', 'inventory:stockOut:delete', 'inventory:stockOut:confirm', 'inventory:stockOut:cancel'
    ]
  }
];

async function main() {
  try {
    console.log('开始初始化权限和角色数据...');

    // 创建权限
    for (const permission of permissions) {
      await prisma.permission.upsert({
        where: { code: permission.code },
        update: permission,
        create: {
          ...permission,
          updatedAt: new Date()
        }
      });
    }
    console.log(`已创建${permissions.length}个权限`);

    // 创建角色并分配权限
    for (const roleData of roles) {
      // 创建或更新角色
      const role = await prisma.role.upsert({
        where: { name: roleData.name },
        update: {
          description: roleData.description,
          isSystem: roleData.isSystem
        },
        create: {
          name: roleData.name,
          description: roleData.description,
          isSystem: roleData.isSystem,
          updatedAt: new Date()
        }
      });

      // 删除该角色的所有现有权限关联
      await prisma.rolePermission.deleteMany({
        where: { roleId: role.id }
      });

      // 为角色分配权限
      for (const permCode of roleData.permissions) {
        const permission = await prisma.permission.findUnique({
          where: { code: permCode }
        });

        if (permission) {
          await prisma.rolePermission.create({
            data: {
              roleId: role.id,
              permissionId: permission.id
            }
          });
        }
      }
    }
    console.log(`已创建${roles.length}个角色并分配权限`);

    console.log('权限和角色数据初始化完成');
  } catch (error) {
    console.error('权限和角色数据初始化失败:', error);
    process.exit(1);
  } finally {
    await prisma.$disconnect();
  }
}

// 如果此文件被直接执行，则运行main函数
if (require.main === module) {
  main();
}

// 导出main函数，以便其他文件可以导入
module.exports = { initPermissionsAndRoles: main }; 